/*
 * Powered By XY
 * Since 2020
 */
package com.xy.biz.fd.client;

import com.xy.biz.fd.domain.req.document.CopyDocumentRequest;
import com.xy.biz.fd.domain.req.document.GetDocumentRequest;
import com.xy.biz.fd.domain.req.document.ListDocumentRequest;
import com.xy.biz.fd.domain.req.document.SaveDocumentRequest;
import com.xy.biz.fd.domain.resp.GetDocumentResponse;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.util.ResultUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 *
 *
 * @author ssl
 * @date 2021-1-23
 */
public interface DocumentClient {

	/**
	 * 保存
	 *
	 * @param request
	 * @return
	 */
	Result<? extends GetDocumentResponse> saveDocument(SaveDocumentRequest request);
	/**
	 * 查找一个
	 *
	 * @param request
	 * @return
	 */
	Result<GetDocumentResponse> getDocument(GetDocumentRequest request);

	/**
	 * 上传文件
	 */
	Result<?> uploadDocument(MultipartHttpServletRequest request);

	/**
	 * 查找多个
	 *
	 * @param collection 标识集合
	 * @return
	 */
	Result<List<GetDocumentResponse>> listDocumentById(Collection<Long> collection);

	/**
	 * 查找多个
	 *
	 * @param request 查找条件
	 * @return
	 */
	Result<List<GetDocumentResponse>> listDocument(ListDocumentRequest request);

	/**
	 * 统计个数
	 *
	 * @param request 查找条件
	 * @return
	 */
	Result<Integer> countDocument(ListDocumentRequest request);

	/**
	 * 分页查找
	 *
	 * @param request 查找条件
	 * @return
	 */
	Result<Page<GetDocumentResponse>> pageDocument(ListDocumentRequest request);

	/**
	 * @author Greekn Zhang
	 * @date 2021/5/10 14:17
	 * @param request
	 * @return com.xy.lang.domain.Result<?>
	 * @description 复制文档
	 */
	Result<GetDocumentResponse> copyDocument(CopyDocumentRequest request);

	Result<? extends Integer> updateDocument(SaveDocumentRequest request);
	Result<?> updateAllDocument(Collection<SaveDocumentRequest> request);
	Result<? extends Integer> updateDocumentByIds(SaveDocumentRequest request);
	default <T> void buildByDocument(Collection<T> source,
									 Collection<Long> documentIds,
									 Function<T, Long> documentIdGetter,
									 BiConsumer<T, GetDocumentResponse> builder
									 ){
		Objects.requireNonNull(documentIdGetter, "The userIdGetter is required.");
		Objects.requireNonNull(builder, "The builder is required.");
		if (CollectionUtils.isEmpty(source)) {
			return;
		}
		if (CollectionUtils.isEmpty(documentIds)) {
			return;
		}
		Set<Long> userIdSet = new HashSet<>(documentIds);
		Result<List<GetDocumentResponse>> listResult = listDocumentById(userIdSet);
		List<GetDocumentResponse> getDocumentResponses = ResultUtils.get(listResult);
		if (!CollectionUtils.isEmpty(getDocumentResponses)) {
			Map<Long, GetDocumentResponse> documentResponseMap =
					getDocumentResponses.stream().collect(Collectors.toMap(resp -> resp.getDocumentId(), resp -> resp));
			for (T t : source) {
				Long documentId = documentIdGetter.apply(t);
				GetDocumentResponse getDocumentResponse = documentResponseMap.get(documentId);
				if (!Objects.isNull(getDocumentResponse)) {
					builder.accept(t,getDocumentResponse);
				}
			}
		}
	}
}
